package com.wufun.overseas.utils;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;
import android.widget.Toast;

import com.wufun.overseas.R;
import com.wufun.overseas.models.bean.IntentDateBean;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yangg on 2017/12/14.
 */

public class AppDevicesUtil {


    /**
     * 跳转谷歌应用市场 跳转市场则走webview
     * JDE170208S100019352
     * 启动到应用商店app详情界面
     *
     * @param appPkg    目标App的包名
     * @param marketPkg 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面，某些应用商店可能会失败
     */
    public static void launchAppDetail(String appPkg, String marketPkg,Context mContext) {
        try {
            if (TextUtils.isEmpty(appPkg) || TextUtils.isEmpty(marketPkg)) {
                Toast.makeText(mContext, R.string.Not_Found_Package_Name, Toast.LENGTH_SHORT).show();
                return;
            }
            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (!TextUtils.isEmpty(marketPkg)) {
                intent.setPackage(marketPkg);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addCategory("android.intent.category.DEFAULT");
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            IntentDateBean intentDateBean = new IntentDateBean();
            intentDateBean.setLink_type_val("http://play.google.com/store/apps/details?id=" + appPkg);
            IntentUtil.getInstance().goShareWebActivity(mContext, intentDateBean);
        }
    }


    /**
     * 获取ip地址
     * @param context
     * @return
     */
    public static String getIPAddress(Context context) {
        NetworkInfo info = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//当前使用2G/3G/4G网络
                try {
                    //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                                return inetAddress.getHostAddress();
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }

            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//当前使用无线网络
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
                return ipAddress;
            }
        } else {
            //当前无网络连接,请在设置中打开网络
            Toast.makeText(context, R.string.Not_NetWork, Toast.LENGTH_SHORT).show();
        }
        return null;
    }

    public static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                (ip >> 24 & 0xFF);
    }


    /**
     * 获取手机系统版本
     * @return
     */
    public static String getOS() {
        String osName="";
        int os = Build.VERSION.SDK_INT;
        switch (os) {
            case 1:
                osName="Android 1.0";
                break;
            case 2:
                osName="Android 1.1";
                break;
            case 3:
                osName="Android 1.5";
                break;
            case 4:
                osName="Android 1.6";
                break;
            case 5:
                osName="Android 2.0";
                break;
            case 6:
                osName="Android 2.0.1";
                break;
            case 7:
                osName="Android 2.1.x";
                break;
            case 8:
                osName="Android 2.2X";
                break;
            case 9:
                osName="Android 2.3.2";
                break;
            case 10:
                osName="Android 2.3.3";
                break;
            case 11:
                osName="Android 3.0.X";
                break;
            case 12:
                osName="Android 3.1.X";
                break;
            case 13:
                osName="Android 3.2";
                break;
            case 14:
                osName="Android 4.0";
                break;
            case 15:
                osName="Android 4.0.3";
                break;
            case 16:
                osName="Android 4.1";
                break;
            case 17:
                osName="Android 4.2";
                break;
            case 18:
                osName="Android 4.3";
                break;
            case 19:
                osName="Android 4.4";
                break;
            case 20:
                osName="Android 4.4W";
                break;
            case 21:
                osName="Android 5.0";
                break;
            case 22:
                osName="Android 5.1";
                break;
            case 23:
                osName="Android 6.0";
                break;
            case 24:
                osName="Android 7.0";
                break;
            case  25:
                osName="Android 7.1.2";
                break;
            case 26:
                osName="Android 8.0";
                break;
        }
        return osName;
    }


}
